package com.biglead.orderservice.controller;

import com.biglead.common.pojo.OrderInfo;
import com.biglead.common.pojo.Result;
import com.biglead.orderservice.service.OrderKafakaService;
import com.biglead.orderservice.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;



     @Value("${server.port}")
     private String serverPort;

    /**
     * @param id 订单id
     * @return 用户
     */
    @GetMapping(value = "/{id}")
    public OrderInfo queryById(@PathVariable("id") Long id) {
        log.info("查询订单信息 port {}",serverPort);
        return orderService.queryById(id);
    }

    /**
     * 返回加密的请求体
     * @param id
     * @return
     */
    @GetMapping(value = "/encrypt/{id}")
    public Result queryEncryptById(@PathVariable("id") Long id) {
        log.info("查询订单信息 port {}",serverPort);
        OrderInfo orderInfo = orderService.queryById(id);
        return Result.okEncryptData(orderInfo);
    }

    /**
     * 乐观锁下单
     * @param userId
     * @param goodsId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/create")
    public Result create(@RequestParam Long userId,@RequestParam Long goodsId) throws Exception {
        log.info("创建订单信息 port {}",serverPort);
        OrderInfo orderInfo = orderService.createWrongOrder(goodsId,userId);
        return Result.okData(orderInfo);
    }
    /**
     * 乐观锁下单
     * @param userId
     * @param goodsId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/createWithRedis")
    public Result createOrderWithLimitAndRedis(@RequestParam Long userId,@RequestParam Long goodsId) throws Exception {
        log.info("创建订单信息 port {}",serverPort);
        OrderInfo orderInfo = orderService.createOrderWithLimitAndRedis(goodsId,userId);
        return Result.okData(orderInfo);
    }
}
